﻿Imports System.IO
Imports System
Imports System.Text
Imports System.Globalization
Public Class Cadena

    Public Shared Function Left(ByVal str As String, ByVal Lenght As Integer) As String

        Dim strValue As String = ""

        If str.Length < 1 Or str.Trim = "" Then
            Return strValue
        End If

        strValue = str.Substring(0, Lenght)

        Return strValue

    End Function
    Public Shared Function Format(ByVal str As String, ByVal Style As String) As String

        Dim strValue As String = ""
        Dim intLenght As Integer = 0

        intLenght = Style.Length
        strValue = Right(Style & str, intLenght)

        Return strValue

    End Function
    Public Shared Function FormatString(ByVal str As String, ByVal Style As String) As String

        Dim strValue As String = ""

        strValue = Microsoft.VisualBasic.Format(str, Style)

        Return strValue

    End Function
    Public Shared Function Mid(ByVal str As String, ByVal Posicion As Integer, ByVal Lenght As Integer) As String

        Dim strValue As String = ""

        strValue = Microsoft.VisualBasic.Mid(str, Posicion, Lenght)

        Return strValue
    End Function
    Public Shared Function Right(ByVal str As String, ByVal Lenght As Integer) As String

        Dim strValue As String = ""

        strValue = Microsoft.VisualBasic.Right(str, Lenght)

        Return strValue
    End Function
    Public Shared Function Split(ByVal cadena As String) As String()

        Dim r() As String
        r = cadena.Split(" ")
        Return r
    End Function
    Public Shared Function ProperCase(ByVal Cadena As String) As String

        Dim str As String = ""
        Dim strValue As String = ""
        Dim strRetorno As String = ""
        Dim strTexto As String = ""
        Dim i As Integer = 0

        strTexto = Cadena

        Dim strMatriz() As String
        strMatriz = strTexto.Split(" ")

        For i = 0 To (strMatriz.Length - 1)
            If strMatriz(i).Length <= 3 Then
                If i = 0 Then
                    strValue = StrConv(strMatriz(i).ToString, VbStrConv.ProperCase)
                Else
                    strValue = strMatriz(i).ToLower
                End If
            Else
                strValue = StrConv(strMatriz(i).ToString, VbStrConv.ProperCase)
            End If
            strRetorno = strRetorno & " " & strValue
        Next
        str = strRetorno.Trim

        Return str

    End Function
    Public Shared Function ArchivoGrabarLinea(ByVal RutaArchivo As String, ByVal Indice As Integer, ByVal Valor As String) As Boolean

        Dim blnValue As Boolean = True

        If Archivo.FileExiste(RutaArchivo) = True Then

            Dim objReader As New StreamReader(RutaArchivo)
            Dim strLinea As String = ""
            Dim strCadena As New ArrayList
            Do
                strLinea = objReader.ReadLine()
                If Not strLinea Is Nothing Then
                    strCadena.Add(strLinea)
                End If
            Loop Until strLinea Is Nothing
            objReader.Close()

            strCadena.Item(Indice - 1) = Valor.Trim

            Dim strLineas() As String
            ReDim strLineas(strCadena.Count - 1)

            strCadena.CopyTo(strLineas)

            File.WriteAllText(RutaArchivo, "")
            File.WriteAllLines(RutaArchivo, strLineas)
        Else
            blnValue = False
        End If

        Return blnValue
    End Function
    Public Shared Function LeerLineaArchivo(ByVal RutaFile As String, ByVal Indice As Integer) As String

        Dim strValue As String = ""

        'validar si existe el archivo
        If Not My.Computer.FileSystem.FileExists(RutaFile) Then
            Return strValue
        End If

        Dim objReader As New StreamReader(RutaFile)
        Dim sLine As String = ""
        Dim arrText As New ArrayList
        Do
            sLine = objReader.ReadLine()
            'sLine = objReader.ReadLine.Normalize()
            If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If

        Loop Until sLine Is Nothing
        objReader.Close()

        If arrText.Count < 1 Then
            strValue = ""
        Else
            Try
                strValue = arrText(Indice - 1).ToString
            Catch ex As Exception
                strValue = ""
            End Try

        End If

        Return strValue

    End Function
    Public Shared Function LeerArchivo(ByVal RutaFile As String) As String

        Dim strValue As String = ""

        'validar si existe el archivo
        If Not My.Computer.FileSystem.FileExists(RutaFile) Then
            Return strValue
        End If

        Dim objReader As New StreamReader(RutaFile)
        Dim sLine As String = ""
        Dim str As New Text.StringBuilder

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                str.AppendLine(sLine)
            End If

        Loop Until sLine Is Nothing
        objReader.Close()

        Return str.ToString

    End Function
    Public Shared Function SinTildes(ByVal MiCadena As String) As String

        Dim strValue As String = ""

        If MiCadena.Trim = "" Or MiCadena.Length < 1 Then
            Return strValue
        End If

        Dim ConTilde As String = "á,é,í,ó,ú,Á,É,Í,Ó,Ú,ñ,Ñ"
        Dim SinTilde As String = "a,e,i,o,u,A,E,I,O,U,n,N"

        Dim intPosCadena As Integer = 0
        Dim strLetra As String = ""
        Dim strReemplazo As String = ""

        For i As Integer = 0 To MiCadena.Length - 1      'verificar todos los bytes de la cadena

            strLetra = MiCadena.Substring(i, 1)    'extraer el byte indicado
            strReemplazo = strLetra

            intPosCadena = InStr(ConTilde, strLetra)    'verificar si el byte es acentuado

            If intPosCadena > 0 Then          'Si es acentuado, entonces
                strReemplazo = Mid(SinTilde, intPosCadena, 1)       'Reemplazarlo por el que no es acentuado
            End If

            strValue = strValue & strReemplazo      'seguir con las demas bytes
        Next

        Return strValue

    End Function
End Class
